import {AsyncStorage} from 'react-native'
import {AuthModel} from "./model";
import {kAuthorization, kContentType} from "../constant";
import Request from "../utils/Request";
import constant from "../utils/constant";
import RouterServer from "../utils/RouterServer";

const {Toast} = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_place";
export default {
    namespace: 'place',
    state: {
        questionTypeList: null
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {


        // 保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        // 本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        //登记访客
        * registeGuest({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            let params = {};
            params[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            params[kContentType] = 'application/json';
            params['params'] = {...payload};

            // console.log(params);
            const result = yield call(Request.post, Request.getApi().guests, params);
            if (result) {
                Toast.success('登记成功', constant.ToastDuration);
                yield call(RouterServer.pop)
            }

        },

        //获取全部问题类型
        * fetchQuestionTypeList({payload}, {call, put, select}) {
            if (!payload) payload = {pi: 1, ps: 100};
            const auth: AuthModel = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const questionTypeList: questionTypeList = yield call(Request.get, Request.getApi().questionType, payload);
            if (questionTypeList) {
                yield put({type: 'save', payload: {questionTypeList}});
            }
        },


        //提交问题
        *reportQuestion({payload},{call,put,select}){
            const auth: AuthModel = yield select(state => state.auth);
            let params = {};
            params[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            params[kContentType] = 'application/json';
            params['params'] = {...payload};


            const from = payload.from;
            delete payload.from;
            const result = yield call(Request.post, Request.getApi().support, params);
            if (result) {
                Toast.success('提交成功', constant.ToastDuration);
                yield call(RouterServer.pop)
            }
        },

        //获取区域以及区域经理信息
        *fetchAreaUser({payload},{call,put,select}){
          const auth:AuthModel = yield select(state=>state.auth);
          if(!payload)payload = {};
          payload[kAuthorization] = auth.token_type+' '+auth.access_token;
          const result = yield call(Request.get,Request.getApi().areaUser+`/${auth.userInfo.company.area.id}`,payload);

        },
    },
    subscriptions: {
        setup({dispatch}) {

        }
    },
};
